/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

/**
 * 会员充值规则
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumnModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TShopCharge;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jpanel.RepeatImagePanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.component.tableSwing.JDatePicker;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.factory.UIFactory;
import com.ruipengkj.model.MemberFillCardChargeModel;
import com.ruipengkj.service.TShopChargeService;
import com.ruipengkj.view.base.BasePagePanel;

public class MemberFillCardChargePanel extends BasePagePanel<TShopCharge> {

	private static final long serialVersionUID = 1L;
	
	private JButton addButton;//添加
	private JButton saveButton;//保存
	private JButton delButton;//删除
	private JButton queryButton;//查询
	
	private PageView<TShopCharge> pageView;
	private PagingComponent pagingComponent;//翻页组件
	
	private MemberFillCardChargeModel tableModel;
	private JTable table;
	private JScrollPane tableJsp;
	
	private TShopChargeService tShopChargeService = BuildSpringApplication.getInstance().getTShopChargeServiceImpl();
	
	public MemberFillCardChargePanel() {
		this.init();
		termQuery(1);
	}
	
	private void init() {
		tableModel = new MemberFillCardChargeModel();
		table = UIFactory.getInstance().createRPJTable(tableModel, null, null, 30, JLabel.CENTER);
		tableJsp = uiFactory.createJScrollPane(table);
		
		TabelUtil.setTableColumnWidths(table, 0, 12);
		TabelUtil.setTableColumnWidths(table, 1, 31);
		TabelUtil.setTableColumnWidths(table, 2, 26);
		TabelUtil.setTableColumnWidths(table, 3, 92);
		TabelUtil.setTableColumnWidths(table, 4, 92);
		TabelUtil.setTableColumnWidths(table, 5, 40);
		TabelUtil.setTableColumnWidths(table, 8, 110);
		TabelUtil.setTableColumnWidths(table, 9, 31);
		
		TableColumnModel columnModel = table.getColumnModel();
		JComboBox editor = new JDatePicker(JDatePicker.STYLE_CN_DATE1);
		columnModel.getColumn(4).setCellEditor(new DefaultCellEditor(new JComboBox(ApplicationContext.MEMBER_CHARGE_TYPE)));
		columnModel.getColumn(6).setCellEditor(new DefaultCellEditor(editor));
		columnModel.getColumn(7).setCellEditor(new DefaultCellEditor(editor));
		
		pageView = new PageView<TShopCharge>();
		pagingComponent = new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		JPanel gardPanel = new RepeatImagePanel(ImageFactory.loadImage(ImageFactory.DIALOG_DOWN_BG_1).getImage());
		gardPanel.add(pagingComponent);
		
		this.setLayout(new BorderLayout());
		this.add(initTop(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(gardPanel, BorderLayout.SOUTH);
		initListener();
	}
	
	private JPanel initTop() {
		addButton = new RPJButtonWhite("添 加");//添加
		saveButton = new RPJButtonWhite("保 存");//保存
		delButton = new RPJButtonWhite("删 除");//删除
		queryButton = new RPJButtonWhite("查 询");//查询
		
		JPanel jp1 = new JPanelFlowLayoutComponent(new Component[]{
				addButton, saveButton, delButton, queryButton
		}, FlowLayout.CENTER);
		return jp1;
	}
	
	//初始化监听器
	private void initListener() {
		addButton.addActionListener(this);
		saveButton.addActionListener(this);
		delButton.addActionListener(this);
		queryButton.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == addButton) {
			//添加
			tableModel.addRow();
		} else if (e.getSource() == saveButton) {
			//保存
			this.save();
		} else if (e.getSource() == delButton) {
			//删除
			this.delete();
		} else if (e.getSource() == queryButton) {
			//查询
			this.termQuery(1);
		} 
	}
	
	//删除选中的数据
	private void delete() {
		List<TShopCharge> tShopChargeList = tableModel.findAllTShopChargeList();
		if (tShopChargeList == null || tShopChargeList.size() == 0) {
			Util.showMessageDialog("请选择需要删除的数据！");
			return;
		}
		boolean isSelect = false;//储存：是否有选中数据
		for (int i = tShopChargeList.size() - 1; i >= 0; --i) {
			TShopCharge tShopCharge = tShopChargeList.get(i);
			if (tShopCharge.isSelect()) {
				if (isSelect == false && Util.showConfirmDialog("确认保存表中选中数据吗？") == 2)
					return;
				isSelect = true;
				try {
					tableModel.delTShopCharge(i);//删除表中数据
					if (tShopCharge.getChargeID() != null && !tShopCharge.getChargeID().equals("")) {
						tShopChargeService.deleteService(tShopCharge.getChargeID());
					}
				} catch (Exception e) {
					Util.showMessageDialog("删除失败！");
					e.printStackTrace();
					return;
				}
			}
		}
		
		if (!isSelect) 
			Util.showMessageDialog("请先选择数据");
		else {
			Util.showMessageDialog("删除成功！");
			termQuery(1);
		}
	}
	
	//保存数据选中的数据
	private void save() {
		List<TShopCharge> tShopChargeList = tableModel.findAllTShopChargeList();
		if (tShopChargeList == null || tShopChargeList.size() == 0) {
			Util.showMessageDialog("当前表中无数据信息，请先执行添加操作！");
			return;
		}
		boolean isSelect = false;//储存：是否有选中数据
		TShopCharge tShopCharge=null;
		//是否保存数据、是否有选中数据、数据是否填写正确
		for (int i =0; i <  tShopChargeList.size(); i++) {
			tShopCharge = tShopChargeList.get(i);
			if (tShopCharge.isSelect()) {
				if (!isSelect) {
					if (Util.showConfirmDialog("确认保存表中选中数据吗？") == 0)
						isSelect = true;
					else 
						return;
				}
				
				if (tShopCharge.getAMount() == 0) {
					Util.showMessageDialog("第"+i+"条数据，充值金额不能为0！");
					return;
				}
				if (tShopCharge.getGiftAmount() == 0) {
					Util.showMessageDialog("第"+i+"条数据，赠送金额/放大率不能为0！");
					return;
				}
			}
		}
		
		if (isSelect){
			for (int i =0; i <  tShopChargeList.size(); i++) {
				tShopCharge = tShopChargeList.get(i);
				if (tShopCharge.isSelect()) {
					boolean isNew = false;
					if (tShopCharge.getChargeID() == null || tShopCharge.getChargeID().equals("")) {
						tShopCharge.setChargeID(UUID.GenUUID15());
						isNew = true;
					}
					try {
						tShopChargeService.saveOrUpdateService(tShopCharge);
						tShopCharge.setSelect(false);
						tableModel.fireTableCellUpdated(i, 1);
						if(isNew){
							BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加充值规则");
						}else{
							BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改充值规则");	
						}
					} catch (Exception e) {
						e.printStackTrace();
						Util.showMessageDialog("第"+i+"条数据，保存失败！\n");
					}
				}
			}
			Util.showMessageDialog("操作成功！\n");
		}else{
			Util.showMessageDialog("请先选择需要保存的数据信息！");
		}
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TShopCharge> pageModel = tShopChargeService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),null);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						tableModel.setTShopChargeList(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						tableModel.clearAllTShopCharge();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TShopCharge> getPageView() {
		return pageView;
	}
	
	@Override
	public void clear() {
		
	}
}
